<?php
class Server {
  public static function submit_obj($orm, $sys, $inst, $is_iframe) {
    $req = json_decode(Utils::get_param('req', '[]'));
    
    if ($req->name == '') {
      echo 'Name is empty';
      return;
    }
    
    if (is_numeric($req->price) == false) {
      echo 'Price must be a number';
      return;
    }
    
    if (is_numeric($req->weight) == false) {
      echo 'Weight must be a number';
      return;
    }
    
    if (isset($req->id) && $req->id != 0) {
      $orm->update('pb_ecom_product', $req);
    } else {
      $orm->add('pb_ecom_product', $req);
      $req->id = $orm->last_id();
    }
    
    if (isset($req->__pdobjs)) {
      foreach ($req->__pdobjs as $itm) {
        $orm->update('pb_ecom_product_obj', $itm);
      }
    }
    
    if (isset($req->__pg)) {
      $req->__objs = $orm->get('pb_ecom_product', 't.name', 'ASC', $req->__pg, SIZE, NULL);
    }
    
    echo json_encode($req);
  }
  
  public static function submit_attr($orm, $sys, $inst, $is_iframe) {
    $req = json_decode(Utils::get_param('req', '[]'));
    
    if ($req->name == '') {
      echo 'Name is empty';
      return;
    }
    
    if (isset($req->id) && $req->id != 0) {
      $orm->update('pb_ecom_product_attr', $req);
    } else {
      $orm->add('pb_ecom_product_attr', $req);
      $req->id = $orm->last_id();
      
      $req->__opt = new stdClass();
      $req->__opt->product_track_id = $req->product_track_id;
      $req->__opt->product_attr_id = $req->id;
      $req->__opt->name = 'Default';
      $req->__opt->file = '';
      $orm->add('pb_ecom_product_attr_opt', $req->__opt);
      $req->__opt->id = $orm->last_id();
    }
    $req->__pdobjs = Server::__get_sycd_objs($orm, $req->product_track_id);
    echo json_encode($req);
  }
  
  public static function delete_attr($orm, $sys, $inst, $is_iframe) {
    $req = json_decode(Utils::get_param('req', '[]'));
    $orm->delete('pb_ecom_product_attr', $req);
    
    $opts = new stdClass();
    $opts->cd = 'product_attr_id = :v1';
    $opts->cdv = array(':v1' => $req->id);
    $orm->delete_by_opts('pb_ecom_product_attr_opt', $opts);
    $req->__pdobjs = Server::__get_sycd_objs($orm, $req->product_track_id);
    echo json_encode($req);
  }
  
  public static function submit_opt($orm, $sys, $inst, $is_iframe) {
    $req = json_decode(Utils::get_param('req', '[]'));
    
    if ($req->name == '') {
      echo 'Name is empty';
      return;
    }
    
    if (isset($req->id) && $req->id != 0) {
      $orm->update('pb_ecom_product_attr_opt', $req);
    } else {
      $orm->add('pb_ecom_product_attr_opt', $req);
      $req->id = $orm->last_id();
    }
    $req->__pdobjs = Server::__get_sycd_objs($orm, $req->product_track_id);
    echo json_encode($req);
  }
  
  public static function delete_opt($orm, $sys, $inst, $is_iframe) {
    $req = json_decode(Utils::get_param('req', '[]'));
    $orm->delete('pb_ecom_product_attr_opt', $req);
    $req->__pdobjs = Server::__get_sycd_objs($orm, $req->product_track_id);
    echo json_encode($req);
  }
  
  public static function delete_image($orm, $sys, $inst, $is_iframe) {
    $req = json_decode(Utils::get_param('req', '[]'));
    $orm->delete('pb_ecom_product_img', $req);
    
    $opts = new stdClass();
    $opts->cd = 't.product_track_id = :v1';
    $opts->cdv = array(':v1' => $req->product_track_id);
    $res = $orm->get('pb_ecom_product_img', 't.id', 'ASC', NULL, NULL, $opts)->d;
    
    if ($req->is_default == 1 && count($res) > 0) {
      $res[0]->is_default = 1;
      $orm->update('pb_ecom_product_img', $res[0]);
    }
    
    echo json_encode($res);
  }
  
  public static function set_image_default($orm, $sys, $inst, $is_iframe) {
    $req = json_decode(Utils::get_param('req', '[]'));
    $orm->exec('UPDATE pb_ecom_product_img SET is_default = 0 WHERE product_track_id = \'' . $req->product_track_id . '\'');
    $orm->update('pb_ecom_product_img', $req);
  }
  
  public static function get_obj($orm, $sys, $inst, $is_iframe) {
    $req = json_decode(Utils::get_param('req', '[]'));
    
    $opts = new stdClass();
    $opts->cd = 't.id = :v1';
    $opts->cdv = array(':v1' => $req->id);
    $res = $orm->get('pb_ecom_product', NULL, NULL, NULL, NULL, $opts)->d;
    if (count($res) > 0) {
      $opts = new stdClass();
      $opts->cd = 't.product_track_id = :v1';
      $opts->cdv = array(':v1' => $res[0]->track_id);
      $res[0]->__images = $orm->get('pb_ecom_product_img', NULL, NULL, NULL, NULL, $opts)->d;
      $res[0]->__attrs = $orm->get('pb_ecom_product_attr', NULL, NULL, NULL, NULL, $opts)->d;
      $res[0]->__opts = $orm->get('pb_ecom_product_attr_opt', NULL, NULL, NULL, NULL, $opts)->d;
      $res[0]->__pdobjs = $orm->get('pb_ecom_product_obj', NULL, NULL, NULL, NULL, $opts)->d;
    }
    echo json_encode($res);
  }
  
  public static function upload_images($orm, $sys, $inst, $is_iframe) {
    if (isset($_FILES['Filedata']) && is_uploaded_file($_FILES['Filedata']['tmp_name']) && $_FILES['Filedata']['error'] == 0) {
      //获得上传文件信息
      $upload_file = $_FILES['Filedata'];
      //获取文件扩展名
      $file_info = pathinfo($upload_file['name']);
      $file_ext = (isset($file_info['extension'])) ? $file_info['extension'] : '';
      
      $updir = UPLOAD_DIR_PLGN . $inst->__folder;
      if (!file_exists($updir)) {
        Utils::create($updir);
      }
        
      //获取客户端参数
      $track_id = Utils::get_param('track_id', 0);
      
      //Add custom code here...
      $opts = new stdClass();
      $opts->cd = 't.is_default = :v1 AND t.product_track_id = :v2';
      $opts->cdv = array(':v1' => 1, ':v2' => $track_id);
      $rslt = $orm->get('pb_ecom_product_img', NULL, NULL, NULL, NULL, $opts)->d;
      
      $file_ext = strtolower((isset($file_info['extension'])) ? $file_info['extension'] : '');
      $res = new stdClass();
      $res->product_track_id = $track_id;
      $res->file = $updir . '/' . Utils::unique() . '.' . $file_ext;
      $res->is_default = (count($rslt) == 0) ? 1 : 0;
      if (move_uploaded_file($upload_file['tmp_name'], $res->file)) {
        $orm->add('pb_ecom_product_img', $res);
        $res->id = $orm->last_id();
        echo json_encode($res);
      }
    }
  }
  
  public static function __get_sycd_objs($orm, $product_track_id) {
    $opts = new stdClass();
    $opts->cd = 't.track_id = :v1';
    $opts->cdv = array(':v1' => $product_track_id);
    $products = $orm->get('pb_ecom_product', NULL, NULL, NULL, NULL, $opts)->d;
    
    $opts = new stdClass();
    $opts->cd = 't.product_track_id = :v1';
    $opts->cdv = array(':v1' => $product_track_id);
    $objs = $orm->get('pb_ecom_product_obj', NULL, NULL, NULL, NULL, $opts)->d;
    $attrs = $orm->get('pb_ecom_product_attr', NULL, NULL, NULL, NULL, $opts)->d;
    $myopts = $orm->get('pb_ecom_product_attr_opt', NULL, NULL, NULL, NULL, $opts)->d;

    $attr_objs = Server::__get_attr_objs($attrs, $myopts);
    
    //Add and update
    foreach ($attr_objs as $itm) {
      $ex = 0;
      foreach ($objs as $itm2) {
        if ($itm->attr_opt_id == $itm2->attr_opt_id) {
          $itm2->attr_opt_code = $itm->attr_opt_code;
          $orm->update('pb_ecom_product_obj', $itm2);
          $ex = 1;
          break;
        }
      }
      if ($ex == 0 && $itm->attr_opt_id != '') {
        $orm->add('pb_ecom_product_obj', $itm);
      }
    }
    
    //Delete
    foreach ($objs as $itm) {
      $ex = 0;
      foreach ($attr_objs as $itm2) {
        if ($itm->attr_opt_id == $itm2->attr_opt_id) {
          $ex = 1;
          break;
        }
      }
      if ($ex == 0) {
        $orm->delete('pb_ecom_product_obj', $itm);
      }
    }
    
    $opts = new stdClass();
    $opts->cd = 't.product_track_id = :v1';
    $opts->cdv = array(':v1' => $product_track_id);
    $objs = $orm->get('pb_ecom_product_obj', NULL, NULL, NULL, NULL, $opts)->d;
    
    //Update alert
    if (count($products) > 0) {
      require_once '../include/ecomutils.class.php';
      EComUtils::update_alert($orm, $products[0], $objs);
    }
    
    return $objs;
  }
  
  public static function __compare($a, $b) {
    return ($a->id > $b->id) ? 1 : -1;
  }
  
  public static function __get_attr_objs($attrs, $opts) {
    usort($attrs, 'Server::__compare');
    usort($opts, 'Server::__compare');
    foreach ($attrs as $itm) {
      $itm->__opts = array();
      foreach ($opts as $itm2) {
        if ($itm2->product_attr_id == $itm->id) {
          $itm->__opts[] = $itm2;
        }
      }
    }
    $rslt = new stdClass();
    $rslt->objs = array();
    Server::__extract_attr_objs($attrs, 0, '', '', $rslt);
    return $rslt->objs;
  }
  
  public static function __extract_attr_objs($attrs, $idx, $id, $cd, $rslt) {
    if (count($attrs) <= $idx && count($attrs) > 0) {
      $obj = new stdClass();
      $obj->product_track_id = $attrs[0]->product_track_id;
      $obj->attr_opt_id = $id;
      $obj->attr_opt_code = $cd;
      $obj->stock = 0;
      $obj->stock_alert = 0;
      $obj->price = 0;
      $rslt->objs[] = $obj;
      return;
    }
    if ($idx > 0) {
      $id .= '-';
      $cd .= '+';
    }
    if (isset($attrs[$idx])) {
      foreach ($attrs[$idx]->__opts as $obj) {
        Server::__extract_attr_objs($attrs, $idx + 1, $id . $obj->id, $cd . $obj->name, $rslt);
      }
    }
  }
}
?>